package com.zk.atm.utils;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

import com.bpms.common.VO;
import com.bpms.common.VOList;
import com.zk.atm.vo.AtmpOrg;

public class JsonUtil {
    public static final Logger logger=Logger.getLogger(JsonUtil.class);
	public static String getJsonObjectByBpmVo(VO vo) throws Exception{
		String json="";
		try {
			Map map=new HashMap();
			Hashtable<String, Object> ht = vo.attrs();
			for (Iterator it = ht.entrySet().iterator(); it.hasNext();) {
				Map.Entry entry = (Map.Entry) it.next();
//				System.out.print(entry.getValue());
//				System.out.print("----======-----"+entry.getKey());
				map.put(entry.getKey().toString().toLowerCase(), entry.getValue());
			}

			String ret=StringUtil.getPropertyValue(vo.getAttr(ConstantsAtmp.ATMP_GATEWAYSERVLET_CONVERT_BPMVO_TO_JSON_RET));
			//给默认值:0;成功;1,失败
			ret=ret.equals("")?"1":ret;
			map.put(ConstantsAtmp.ATMP_GATEWAYSERVLET_CONVERT_BPMVO_TO_JSON_RET, ret);
			String message=StringUtil.getPropertyValue(vo.getAttr(ConstantsAtmp.ATMP_GATEWAYSERVLET_CONVERT_BPMVO_TO_JSON_MESSAGE));
			message=message.equals("")?"ERROR":message;
			map.put(ConstantsAtmp.ATMP_GATEWAYSERVLET_CONVERT_BPMVO_TO_JSON_MESSAGE, message);
			// json 解析bpm voList
			VOList voList=(VOList) vo.getObj(ConstantsAtmp.ATMP_GATEWAYSERVLET_CONVERT_BPMVO_TO_JSON_VOLIST);
			if(voList==null){
				voList=new VOList();
			}
			//获取VOlist中的vector
			Vector<VO> v = voList.listData;
			if(v==null||v.size()==0){
				v=new Vector<VO>();
			}
			JSONArray ja = new JSONArray();
			//获取vector中的Bpm的vo，进行序列化
			for (VO vo1 : v) {
				ja.put(vo1.attrs());
			}
			map.put(ConstantsAtmp.ATMP_GATEWAYSERVLET_CONVERT_BPMVO_TO_JSON_VOLIST, ja);
			JSONObject json_str=JSONObject.fromObject(map);
			json=json_str.toString();
			if(logger.isDebugEnabled()){
				System.out.println("*****************json="+json);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(new Date().toLocaleString()+"\r\n解析BPMVo序列成json报错！"+e.getMessage());
		}
		return json;
	}
	
	public static void main(String[] args) {
		JsonUtil.testConvertBpmListVoToJson();
//		VO vo=new VO();
//		try {
//			JdbcWorker jdbc=new JdbcWorker();
//			VOList voList=jdbc.readAll(new VO(), "select *from atmp_org");
//			
//			vo.setAttr(ConstantsAtmp.ATMP_GATEWAYSERVLET_CONVERT_BPMVO_TO_JSON_RET, "0");
//			vo.setAttr(ConstantsAtmp.ATMP_GATEWAYSERVLET_CONVERT_BPMVO_TO_JSON_MESSAGE, "success");
//			vo.setObj(ConstantsAtmp.ATMP_GATEWAYSERVLET_CONVERT_BPMVO_TO_JSON_VOLIST,voList );
//			JsonUtil.getJsonObjectByBpmVo(vo);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
		
	}
	
	public static void testConvertBpmListVoToJson(){
		Map map=new HashMap();
		List voList=new ArrayList();
		AtmpOrg org=new AtmpOrg();
		org.setOrg_id("1");
		org.setOrg_code("11");
		voList.add(org);
		
		org.setOrg_id("2");
		org.setOrg_code("22");
		voList.add(org);
		map.put("ret", "0");
		map.put("message", "success");
		map.put("data", voList);
		JSONObject json=JSONObject.fromObject(map);
		System.out.println("json="+json);
	}
}
